package com.lhy.tool.generator;

import com.lhy.tool.autoconfigation.GeneratorConfiguration;
import com.lhy.tool.model.CrudBean;
import com.lhy.tool.model.factory.CrudBeanFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;

@Slf4j
public class GeneratorHelperRunner implements CommandLineRunner {

    @Autowired(required = false)
    List<Generator> generators;

    @Autowired
    CrudBeanFactory crudBeanFactory;

    @Autowired
    GeneratorConfiguration generatorConfiguration;

    @Override
    public void run(String... args) throws Exception {
        log.info("增删改查代码开始生成");
        try {
            List<CrudBean> crudBeans = crudBeanFactory.buildCrudBeans();
            crudBeans.forEach(crudBean -> {
                Assert.notNull(crudBean.getTable(), "未匹配到数据表：" + crudBean.getTableName());
                generators.forEach(generator -> generator.genCode(crudBean, generatorConfiguration));
            });
        } catch (Exception e) {
            log.error("代码生成失败", e);
        }
        log.info("生成结束");
    }
}
